
const { ccclass, property } = cc._decorator;

@ccclass
/**
 * 动画管理辅助类
 * 
 * 维护切后台停止的动画
 * 
 * 使用addAction 替代 runAction
 * 
 * 
 */
class ActionManagerHelperHall {
    /** 是否初始化过 */
    isInit = false;

    /**
     * 后台事件;
     */
    _addGameEvent() {
        this.isInit = true;
        cc.game.on(
            cc.game.EVENT_HIDE,
            () => {

            }
        );
        cc.game.on(
            cc.game.EVENT_SHOW,
            () => {
                let nowTime = new Date().getTime();
                let AM = cc.director.getActionManager();
                let len = AM._arrayTargets.length
                for (let i = len - 1; i >= 0; i--) {
                    let D = AM._arrayTargets[i]
                    let actions = D.actions
                    let len2 = actions.length;
                    for (let j = 0; j < len2; j++) {
                        let action = actions[j];
                        //如果不是this.addAction 调用的action 不做处理
                        if (!action.createTime) {

                            continue;
                        }
                        let t = nowTime - action.createTime
                        if (action) {
                            action.step(t / 1000)
                        }
                    }
                }
            }
        );
    }
    /** 添加一个需要维护的动画
     * 
     * @param action 
     * @param node 
     */
    addAction(action: cc.Action, node: cc.Node) {
        if (!this.isInit) {
            this._addGameEvent();
        }
        node.runAction(action)
        action.createTime = new Date().getTime()
        action.step(0)
    }

}
export default new ActionManagerHelperHall()